package activities;

import utils.AppPreferences;
import utils.CashData;
import adapters.DrawerAdapter;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.gms.internal.cu;
import com.mga.wheelcommunity.R;

import fragments.FriendsFragment;
import fragments.MapFragment;
import fragments.UserFragment;

public class DrawerActivity extends ActionBarActivity {

	DrawerLayout drawerLayout;
	String[] mDrawerTitles;
	Integer[] images;
	ListView mDrawerList;
	ActionBarDrawerToggle mDrawerToggle;
	FragmentManager fragmentManager;
	Fragment currentFragment;
	Fragment userFragment, mapFragment, friendsFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.drawer);

		mDrawerTitles = getResources().getStringArray(R.array.drawer_titles);
		images = new Integer[] { R.drawable.profile, R.drawable.list,
				R.drawable.map, R.drawable.tracks, R.drawable.logout };

		fragmentManager = getSupportFragmentManager();
		Fragment fragment = new UserFragment();

		UserFragment.setContext(this);
		fragmentManager.beginTransaction()
				.add(R.id.content_frame, fragment).commit();
		
		userFragment = currentFragment = fragment;

		drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);

		mDrawerList.setAdapter(new DrawerAdapter(this, R.layout.drawer_item,
				mDrawerTitles, images));

		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
		mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close);

		drawerLayout.setDrawerListener(mDrawerToggle);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(false);

	}

	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView parent, View view, int position,
				long id) {
			selectItem(position);
		}
	}

	private void selectItem(int position) {
		// Highlight the selected item, update the title, and close the drawer
		mDrawerList.setItemChecked(position, true);
		setTitle(mDrawerTitles[position]);
		drawerLayout.closeDrawer(mDrawerList);
		fragmentManager.beginTransaction().remove(currentFragment).commit();
		switch (position) {
		case 0:
			if(userFragment==null) {
				userFragment = new UserFragment();
				UserFragment.setContext(this);
			}
			
			fragmentManager.beginTransaction()
						.add(R.id.content_frame, userFragment).commit();
			currentFragment = userFragment;
			break;
		case 1:
			if(friendsFragment == null)
				friendsFragment = new FriendsFragment();
			fragmentManager.beginTransaction()
					.add(R.id.content_frame, friendsFragment).commit();
			currentFragment = friendsFragment;
			break;
		case 2:
			if(mapFragment == null)
				mapFragment = new MapFragment();
			fragmentManager.beginTransaction()
					.add(R.id.content_frame, mapFragment).commit();
			currentFragment = mapFragment;
			break;
		case 4:
			AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle(R.string.alert_title);
			alert.setMessage(R.string.alert_text);
			alert.setPositiveButton(R.string.alert_yes, new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					AppPreferences.clearPref(DrawerActivity.this);
					CashData.getInstance().clearCash();
					Intent i = new Intent();
					i.setClass(DrawerActivity.this, LoginActivity.class);
					startActivity(i);
					finish();
				}
			});
			alert.setNegativeButton(R.string.alert_no, null);
			alert.show();
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.drawer, menu);
		return true;
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Pass the event to ActionBarDrawerToggle, if it returns
		// true, then it has handled the app icon touch event
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			Log.d("DrawerActivity", "OnOptionsItemSelected");
			return true;
		}
		// Handle your other action bar items...

		return super.onOptionsItemSelected(item);
	}

}
